<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html; charset=UTF-8" %>
<%
	String action = request.getParameter("action");
	String id = request.getParameter("id");

	if ("ADD".equals(action)) {
		User u = new User();
		u.setUserName("");
		u.setFirstName("");
		u.setLastName("");
		u.setEmail("");
		u.setPassword("");
		u.setAdmin(false);
		
		SessionUtil.setUser(session, u);
		
	} else if ("EDIT".equals(action)) {
		Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
		User u = (User) hSession.get(User.class, Long.valueOf(id));
		
		SessionUtil.setUser(session, u);
	}

	User user = SessionUtil.getUser(session);
	Boolean creation = (user.getId() == null);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
	<% if (creation) { %>
	<title>Bookstore :: Back office :: Ajouter un utilisateur</title>
	<% } else { %>
	<title>Bookstore :: Back office :: Modifier un utilisateur</title>
	<% } %>
	<%@include file="include.jsp" %>
</head>

<body>
	<%@include file="commun.jsp" %>
	
	<div class="contenuAvecMenu">
		<% if (creation) { %>
		<h2>Ajouter un utilisateur</h2>
		<% } else { %>
		<h2>Modifier un utilisateur</h2>
		<% } %>
		<form method="post" action="SaveUser.do">
			<div class="boite bleue">
				<p>
					<label for="userName">Nom d'utilisateur</label>
					<input type="text" id="userName" name="userName" value="<%=user.getUserName()%>" />
				</p>
				
				<% if (creation) { %>
				<p>
					<label for="password">Mot de passe</label>
					<input type="password" id="password" name="password" value="<%=user.getPassword()%>" />
				</p>
				<% } %>
				
				<p>
					<label for="lastName">Nom</label>
					<input type="text" id="lastName" name="lastName" value="<%=user.getLastName()%>" />
				</p>
				
				<p>
					<label for="firstName">Prenom</label>
					<input type="text" id="firstName" name="firstName" value="<%=user.getFirstName()%>" />
				</p>
				
				<p>
					<label for="email">E-mail</label>
					<input type="text" id="email" name="email" value="<%=user.getEmail()%>" />
				</p>
				
				<p>
				<label for="admin">Administrateur</label>
				<% if (user.getAdmin()) { %>
					<input checked="checked" type="radio" id="admin" name="admin" value="true" /> Oui
					<input type="radio" name="admin" value="false" /> Non
				<% } else { %>
					<input type="radio" id="admin" name="admin" value="true" /> Oui
					<input checked="checked" type="radio" name="admin" value="false" /> Non
				<% } %>
				</p>
				
				<input type="submit" value="Sauvegarder" />
			</div>
		</form>
	</div>
</body>
</html>
